<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员分组
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
			array('op'=>'list','title'=>'分组列表'),
			array('op'=>'edit','title'=>'添加分组'),
	);

//会员分组
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','title','desc');
	$list=pdo_getslice('jy_weishop_member_group',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'title','title'=>'分组名称','type'=>'string'),
			array('name'=>'desc','title'=>'描述','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'分组名称','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'select','options'=>'status2'),
		)
	);
	$return['status']=array('0'=>'关闭','1'=>'启用');
	$return['status2']=array(array('id'=>0,'title'=>'关闭'),array('id'=>1,'title'=>'启用'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_member_group',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回分组列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'分组名称','name'=>'title','help'=>''),
					array('type'=>'text','title'=>'描述','name'=>'desc','help'=>''),
					
				)
			),
		)
	);
	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'desc'=>trim($postdata['desc']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_weishop_member_group',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_weishop_member_group',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			$issetid=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'showgroups like'=>'%'.$id.'%'),'id');
			if($issetid){
				exi('该会员组下面已经有商品绑定，不能删除！','error');
			}
			pdo_delete('jy_weishop_member_group',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}